// 49. 字母异位词分组
// https://leetcode.cn/problems/group-anagrams/description/
/**
 * 
示例 1:

输入: strs = ["eat", "tea", "tan", "ate", "nat", "bat"]
输出: [["bat"],["nat","tan"],["ate","eat","tea"]]
示例 2:

输入: strs = [""]
输出: [[""]]
示例 3:

输入: strs = ["a"]
输出: [["a"]]
 */

/**
 * @param {string[]} strs
 * @return {string[][]}
 */
var groupAnagrams = function (strs) {
    let arr = {};
    strs.forEach((str) => {
        let sortStr = str
            .split('')
            .sort((a, b) => {
                if (a < b) return -1;
                else if (a > b) return 1;
                return 0;
            })
            .join('');

        if (arr[sortStr] != null) {
            arr[sortStr].push(str);
        } else {
            arr[sortStr] = [str];
        }
    });

    let ret = [];

    Object.values(arr).forEach((e) => {
        ret.push(e);
    });

    console.log(JSON.stringify(ret));

    return ret;
};

groupAnagrams(['eat', 'tea', 'tan', 'ate', 'nat', 'bat']);
groupAnagrams(['']);
groupAnagrams(['a']);
